package com.qf.ebook.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.ebook.entity.EbookEntry;
import com.qf.ebook.mapper.EbookEntryMapper;
import com.qf.ebook.service.EbookEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Administrator
 * @version V1.0
 * @Project spring_ebook
 * @Package com.qf.ebook.service.impl
 * @Description: ${TODO}
 * @Date 2021/10/2 16:22
 */

@Service
public class EbookEntryServiceImpl implements EbookEntryService {

    @Autowired
    private EbookEntryMapper ebookEntryMapper;

    @Override
    @Transactional
    public void insert(EbookEntry ebookEntry) {
        int i = ebookEntryMapper.insertSelective(ebookEntry);
        if (i != 1){
            System.out.println("添加失败");
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    public void deleteById(Integer id) {
        int i = ebookEntryMapper.deleteByPrimaryKey(id);
        if (i != 1){
            System.out.println("删除失败");
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public void update(EbookEntry ebookEntry) {
        int i = ebookEntryMapper.updateByPrimaryKeySelective(ebookEntry);
        if (i != 1){
            System.out.println("修改失败");
            throw new RuntimeException("修改失败");
        }
    }

    @Override
    public EbookEntry findById(Integer id) {
        return ebookEntryMapper.selectById(id);
    }

    @Override
    public PageInfo<EbookEntry> findByCategoryId(Integer page, Integer rows, Integer ebookCategoryId) {
        PageHelper.startPage(page, rows);
        List<EbookEntry> list = ebookEntryMapper.findByCategoryId(ebookCategoryId);
        PageInfo<EbookEntry> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
